package cn.tedu.project.oa.announcement.controlller;

import cn.tedu.project.oa.announcement.pojo.dto.AnnouncementQuery;
import cn.tedu.project.oa.announcement.pojo.dto.AnnouncementSaveParam;
import cn.tedu.project.oa.announcement.pojo.vo.AnnouncementVO;
import cn.tedu.project.oa.announcement.service.AnnouncementService;
import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.base.response.PageData;
import cn.tedu.project.oa.base.webSocket.CustomWebSocketEndpoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shade.com.alibaba.fastjson2.JSON;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/oa/announcement")
@Api(tags = "05.首页模块-公告")
public class AnnouncementController {

    @Autowired
    AnnouncementService announcementService;

    @ApiOperation("查询公告列表")
    @GetMapping("select")
    public JsonResult selectList(AnnouncementQuery announcementQuery){
      log.debug("查询公告列表:announcementQuery={}",announcementQuery);

        PageData data = announcementService.selectList(announcementQuery);
//      List<AnnouncementVO> list = announcementService.selectList(announcementQuery);

      return JsonResult.ok(data);
    }

    @ApiOperation("显示公告详情")
    @GetMapping("show")
    public JsonResult showAnnouncement(Long id){
        log.debug("显示公告详情:id={}",id);

        AnnouncementVO announcementVO = announcementService.showAnnouncement(id);

        return JsonResult.ok(announcementVO);
    }

    @ApiOperation("发布公告")
    @PostMapping("save")
    public JsonResult saveAnnouncement(AnnouncementSaveParam announcementSaveParam){
        log.debug("发布公告:announcementSaveParam={}",announcementSaveParam);

        announcementService.saveAnnouncement(announcementSaveParam);

        return JsonResult.ok();
    }

    @ApiOperation("删除公告")
    @PostMapping("delete/{id}/{userId}")
    public JsonResult deleteAnnouncement(@PathVariable Long id,@PathVariable Long userId){
        log.debug("删除公告:id={},userId={}",id,userId);

        announcementService.deleteById(id);

        return JsonResult.ok();

    }

    //TODO被Token拦截站内信测试
    @GetMapping("addNum")
    @ResponseBody
    public String sendMessage() {
        String userId = "1";
        Map<String,String> map = new HashMap<>();
        map.put("message","新消息");
        String message = "{message : '新消息'}";

        try {
            CustomWebSocketEndpoint.sendMessageToUser(userId, JSON.toJSONString(map));
            return "发送消息成功:" + userId;
        } catch (IOException e) {
            e.printStackTrace();
            return "发送消息失败";
        }
    }
}
